import Director from '../director'
import store from '../base/store'
import Sprite from '../base/sprite'

/**
 * 障碍物精灵
 */
export class ObstacleSprite extends Sprite {
  constructor(public key: string) {
    const image = Sprite.getMap('sprites')
    const imageData = Sprite.getSpritesData(key)
    const dh = store.canvas.height / 2
    super(
      image,
      imageData.sx,
      imageData.sy,
      imageData.sw,
      imageData.sh,
      store.canvas.width,
      0,
      imageData.sw * (dh / imageData.sh),
      dh
    )
  }

  /**
   * 绘制函数：上下障碍物循环向左移动
   */
  public override draw(): void {
    // 当前障碍物图在canvas的x坐标
    this.x -= Director.getInstance().speed
    super.draw()
  }
}

/**
 * 上障碍物精灵
 */
export class UpObstacleSprite extends ObstacleSprite {
  constructor(private top: number) {
    super('upObstacle')
    // 资源在canvas的y坐标（左上角） = 上障碍物和顶部的距离 - 资源在canvas的高度
    this.y = this.top - this.h
  }
}

/**
 * 下障碍物精灵
 */
export class DownObstacleSprite extends ObstacleSprite {
  constructor(private top: number) {
    super('downObstacle')
    // 资源在canvas的y坐标（左上角） = 上障碍物和顶部的距离 + 上下障碍物之间的间隔
    this.y = this.top + Director.getInstance().gap
  }
}
